package com.example.springboot.controller;

import com.example.springboot.entity.Outstore;
import com.example.springboot.mapper.DeviceMapper;
import com.example.springboot.mapper.OutstoreMapper;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.service.OutstoreService;
import com.example.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/outstores")
public class OutstoreController {

    @Autowired
    private OutstoreMapper outstoreMapper;

    @Autowired
    private OutstoreService outstoreService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    //用get请求查询数据库信息
    @GetMapping
    public List<Outstore> findAll(){
        return outstoreMapper.findAll();//查询所有数据
    }

    //用post请求对数据库数据进行增加和修改
    @PostMapping
    public Integer save(@RequestBody Outstore outstore){
        outstore.setDeviceId(deviceMapper.selectByNumber(outstore.getDeviceNumber()));
        outstore.setApplierId(userMapper.selectId(outstore.getApplierName()));
        outstore.setDealerId(userMapper.selectId(outstore.getDealerName()));
        return outstoreService.save(outstore);//往数据库插入或更新数据
    }


}
